package com.whz.governance.sensitive;

import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
import com.alibaba.nacos.api.PropertyKeyConst;

import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * sensitive store configure for nacos
 *
 * @author whz
 */
public class SensitiveNacosConfig {

    private final ExecutorService pool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<Runnable>(1), new NamedThreadFactory("sensitive-nacos-store-update"),
            new ThreadPoolExecutor.DiscardOldestPolicy());

    private final NacosConfigProperties nacosConfigProperties;

    public SensitiveNacosConfig(final NacosConfigProperties nacosConfigProperties) {
        this.nacosConfigProperties = nacosConfigProperties;
    }

    public String getDataId(SensitiveEnum sensitiveEnum) {
        return sensitiveEnum.getDataId();
    }

    public Properties getServerAddress() {
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.SERVER_ADDR, nacosConfigProperties.getServerAddr());
        properties.setProperty(PropertyKeyConst.NAMESPACE, nacosConfigProperties.getNamespace());
        return properties;
    }

    public String getGroupId() {
        return nacosConfigProperties.getGroup();
    }

    public long getTimeOut() {
        return nacosConfigProperties.getTimeout();
    }

    public ExecutorService getExecutorService(){
        return pool;
    }
}